package com.woniu113.junk.site.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileTypeUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu113.junk.commons.model.Result;
import com.woniu113.junk.site.dto.GoodsInfoDTO;
import com.woniu113.junk.site.form.goodsinfo.AddGoodsInfoForm;
import com.woniu113.junk.site.form.goodsinfo.AllGoodsInfoForm;
import com.woniu113.junk.site.form.goodsinfo.UpdateGoodsInfoForm;
import com.woniu113.junk.site.form.wastetype.AllWasteTypeForm;
import com.woniu113.junk.site.model.GoodsInfo;
import com.woniu113.junk.site.model.WasteType;
import com.woniu113.junk.site.param.goodsinfo.AddGoodsInfoParam;
import com.woniu113.junk.site.param.goodsinfo.AllGoodsInfoParam;
import com.woniu113.junk.site.param.goodsinfo.UpdateGoodsInfoParam;
import com.woniu113.junk.site.param.wastetype.AllWasteTypeParam;
import com.woniu113.junk.site.service.GoodsInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 蜡笔没了小新
 * @since 2024年10月16日
 */
@RestController
@RequestMapping("/goodsInfo")
@Api(value = "GoodsInfoController", tags = "商品信息接口")
public class GoodsInfoController {
    @Resource
    private GoodsInfoService goodsInfoService;

    /**
     * 新增商品信息
     * @param form :需要新增的数据
     * @return 返回新增的商品信息、成功信息和code
     */
    @PostMapping("/addGoodsInfo")
    @ApiOperation(value = "新增商品信息")
    public Result<GoodsInfo> addGoodsInfo(AddGoodsInfoForm form) throws Exception {
        System.out.println("图片："+form.getGoodsImage());
        System.out.println("商品内容"+form.getGoodsContent());
        System.out.println("商品类型"+form.getGoodsType());
        System.out.println("商品名称"+form.getGoodsName());
        System.out.println("库存"+form.getNumber());
        System.out.println("所需积分"+form.getNeedIntegral());
        //通过文件头信息判断文件类型
        String type = FileTypeUtil.getType(form.getGoodsImage().getInputStream());//得到传入文件的文件类型(需要得到传入的image的输入流)
        System.out.println("图片类型："+type);
        if(!(type.equals("jpg") || type.equals("png"))){//不是这两种类型,表示传入的文件不合法
            throw new Exception("图片类型错误");
        }
        GoodsInfo newGoodsInfo = goodsInfoService.addGoodsInfo(BeanUtil.toBean(form, AddGoodsInfoParam.class));
        return Result.getResult(GoodsInfo.class).setData(newGoodsInfo).setMsg("新增商品信息成功").setCode(200);
//        return Result.getResult(GoodsInfo.class).setMsg("成功");
    }

    /**
     * 上架商品(修改商品状态,填入上架数量)
     * @param goodsId 上架商品id
     * @param upNumber 上架数量
     * @return 返回成功提示和code
     */
    @GetMapping("/upGoodsInfo")
    @ApiOperation(value = "上架商品信息")
    public Result upGoodsInfo(@RequestParam("goodsId") Integer goodsId,@Param("upNumber") Integer upNumber){
        System.out.println("商品id"+goodsId);
        System.out.println("上传数量"+upNumber);
        goodsInfoService.upGoodsInfo(goodsId,upNumber);
        return Result.getResult().setMsg("上架商品成功").setCode(200);
    }

    /**
     * 下架商品(修改商品状态,修改上架数量为0)
     * @param goodsId 下架商品id
     * @return 返回成功提示和code
     */
    @GetMapping("/downGoodsInfo")
    @ApiOperation(value = "下架商品信息")
    public Result downGoodsInfo(@RequestParam("goodsId") Integer goodsId){
        System.out.println("商品id"+goodsId);
        goodsInfoService.downGoodsInfo(goodsId);
        return Result.getResult().setMsg("下架商品成功").setCode(200);
    }

    /**
     * 删除商品(修改商品状态)
     * @param goodsId 删除商品id
     * @return 返回成功提示和code
     */
    @GetMapping("/deleteGoodsInfo")
    @ApiOperation(value = "删除商品信息")
    public Result deleteGoodsInfo(@RequestParam("goodsId") Integer goodsId){
        System.out.println("商品id"+goodsId);
        goodsInfoService.deleteGoodsInfo(goodsId);
        return Result.getResult().setMsg("删除商品成功").setCode(200);
    }

    /**
     * 修改商品
     * @param form 修改商品的数据
     * @return 返回成功提示和code
     */
    @PostMapping("/updateGoodsInfo")
    @ApiOperation(value = "修改商品信息")
    public Result updateGoodsInfo(@RequestBody UpdateGoodsInfoForm form){
        goodsInfoService.updateGoodsInfo(BeanUtil.toBean(form, UpdateGoodsInfoParam.class));
        return Result.getResult().setMsg("修改商品成功").setCode(200);
    }

    /**
     * 根据商品id查询商品信息
     * @param goodsId:商品id
     * @return 返回查询出的商品数据、成功提示和code
     */
    @GetMapping("/findByGoodsId")
    @ApiOperation(value = "根据商品id查询商品信息")
    public Result<GoodsInfo> findByGoodsId(@RequestParam("goodsId") Integer goodsId){
        GoodsInfo goodsInfo = goodsInfoService.findByGoodsId(goodsId);
        return Result.getResult(GoodsInfo.class).setData(goodsInfo).setMsg("按商品id查询商品成功").setCode(200);
    }

    /**
     * 查询所有商品信息
     * @param form 要查询商品信息的条件
     * @return 返回查询出的商品数据、成功提示和code
     */
    @PostMapping("/findAll")
    @ApiOperation(value = "查询所有商品信息")
    public Result<List<GoodsInfo>> findAll(@RequestBody AllGoodsInfoForm form) {
        System.out.println(form.getPageNum());
        System.out.println(form.getPageSize());
        GoodsInfoDTO dto = goodsInfoService.findAll(BeanUtil.toBean(form, AllGoodsInfoParam.class));
        return Result.getListResult(GoodsInfo.class).setData(dto.getList()).setMsg("查询所有商品成功").setCode(200).setTotal(dto.getTotal());
    }

    /**
     * 查询所有商品信息(管理端使用)
     * @param form 要查询商品信息的条件
     * @return 返回查询出的商品数据、成功提示和code
     */
    @PostMapping("/findAllByAdmin")
    @ApiOperation(value = "查询所有商品信息")
    public Result<List<GoodsInfo>> findAllByAdmin(@RequestBody AllGoodsInfoForm form) {
        System.out.println(form.getPageNum());
        System.out.println(form.getPageSize());
        GoodsInfoDTO dto = goodsInfoService.findAllByAdmin(BeanUtil.toBean(form, AllGoodsInfoParam.class));
        return Result.getListResult(GoodsInfo.class).setData(dto.getList()).setMsg("查询所有商品成功").setCode(200).setTotal(dto.getTotal());
    }

    /**
     * 根据商品ID修改商品库存(用于用户积分兑换时)
     * @param goodsId:商品id
     * @param reduceNumber:减少的库存量
     * @return 返回成功提示和code
     */
    @GetMapping("/updateNumberByGoodsId")
    @ApiOperation(value = "根据商品ID修改商品库存(用于用户积分兑换时)")
    public Result updateNumberByGoodsId(@RequestParam("goodsId") Integer goodsId,@RequestParam("reduceNumber") Integer reduceNumber){
        goodsInfoService.updateNumberByGoodsId(goodsId,reduceNumber);
        return Result.getResult().setMsg("按商品id修改商品库存成功").setCode(200);
    }
}

